/**
 * Created with hong.rong
 * Description:*
 * Date:2020/06/21
 */

// https://umijs.org/config/
import { defineConfig, utils } from 'umi'
import webpackPlugin from './webpackPlugin'
import routes from './routes'
import defaultSettings from './defaultSettings'

const { winPath } = utils

export default defineConfig({
  hash: true,
  antd: {},
  alias: {
    config: 'config',
  },
  dva: {
    hmr: true,
  },
  locale: {
    // default zh-CN
    default: 'zh-CN',
    // default true, when it is true, will use `navigator.language` overwrite default
    antd: true,
    baseNavigator: true,
  },
  dynamicImport: {
    loading: '@/components/PageLoading/index',
  },
  targets: {
    ie: 11,
  },
  // umi routes: https://umijs.org/docs/routing
  routes,
  // Theme for antd: https://ant.design/docs/react/customize-theme-cn
  theme: {
    // ...darkTheme,
    'primary-color': defaultSettings.primaryColor,
  },
  define: {},
  ignoreMomentLocale: true,
  lessLoader: {
    javascriptEnabled: true,
  },
  cssLoader: {
    modules: {
      getLocalIdent: (context, _, localName) => {
        if (
          context.resourcePath.includes('node_modules') ||
          context.resourcePath.includes('ant.design.pro.less') ||
          context.resourcePath.includes('global.less')
        ) {
          return localName
        }
        const match = context.resourcePath.match(/src(.*)/)
        if (match && match[1]) {
          const qqlPath = match[1].replace('.less', '')
          const arr = winPath(qqlPath)
            .split('/')
            .map(a => a.replace(/([A-Z])/g, '-$1'))
            .map(a => a.toLowerCase())
          return `qql${arr.join('-')}-${localName}`.replace(/--/g, '-')
        }
        return localName
      },
    },
  },
  manifest: {
    basePath: '/',
  },
  // proxy: {
  //   '/api/': {
  //     target: '',
  //     changeOrigin: true,
  //     pathRewrite: {
  //       '^': '',
  //     },
  //   },
  // },
  chainWebpack: webpackPlugin,
})
